package com.miao.teacher.adapter;

import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.graphics.drawable.AnimationDrawable;
import com.miao.teacher.models.NewRecordItem;
import com.miao.teacher.models.RecordItem;
import com.miao.teacher.presenters.OnInputChange;
import com.miao.teacher.ui.R;
import com.miao.teacher.utils.SubMediaPlayer;
import com.miao.teacher.utils.TMediaPlayer;
import android.os.Handler;
import android.os.Message;

public class ReviseAudioAdapter extends BaseAdapter {

	private Context context = null;
	private List<RecordItem> mList = null;
	private double mScreenWidth;
	private LayoutInflater mInflater;
	private AnimationDrawable statusAnim = null;

	private ImageView oldImg = null;

	private int totalLength=0;
	
	private TMediaPlayer mPlayer = new TMediaPlayer();
	private SubMediaPlayer mSubPlayer=new SubMediaPlayer();
	private List<NewRecordItem> mNewList = new ArrayList<NewRecordItem>();

	
	private OnInputChange mListener=null;
	public ReviseAudioAdapter(Context c, List<RecordItem> l,OnInputChange listener) {
		context = c;
		mList = l;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		DisplayMetrics dm = c.getResources().getDisplayMetrics();
		mScreenWidth = dm.widthPixels * 0.5;
		mListener=listener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mNewList == null) {
			return 0;
		}
		if(mNewList.size()>0)
		{
			if(mNewList.get(mNewList.size()-1).isLeft==1)
			{
				return mNewList.size()-1;
			}else
			{
				return mNewList.size();
			}
		}else{
			return mNewList.size();
		}
	}
	public void setTotlaLength(int t)
	{
		totalLength=t;
	}
	
	public void setUrl(String url)
	{
		mSubPlayer.setMediaFile(url, mHandler);
	}
	public void resetNewItems() {
		mNewList.clear();
		if (mList != null && mList.size() > 0) {
			/*************** 处理第一个点 *********************/
			RecordItem oldItem = mList.get(0);
			if (oldItem.starttime == 0) {
				NewRecordItem item = new NewRecordItem();
				item.isLeft = 0;
				item.audio=oldItem;
				item.length = oldItem.getDuration();
				item.startTime = 0;
				mNewList.add(item);
			} else {
				NewRecordItem item = new NewRecordItem();
				item.isLeft = 1;
				item.length = oldItem.starttime;
				item.startTime = 0;
				mNewList.add(item);
				NewRecordItem item2 = new NewRecordItem();
				item2.isLeft = 0;
				item2.audio=oldItem;
				item2.length = oldItem.getDuration();
				item2.startTime = oldItem.starttime;
				mNewList.add(item2);
			}
			/*************** 处理其他点 *****************/
			for (int i = 1; i < mList.size() - 1; i++) {
				NewRecordItem item = new NewRecordItem();
				item.isLeft = 1;
				item.startTime = oldItem.starttime;
				item.length =  mList.get(i).starttime-oldItem.starttime;
				mNewList.add(item);
				NewRecordItem item2 = new NewRecordItem();
				item2.isLeft = 0;
				item2.audio=mList.get(i);
				item2.length = mList.get(i).getDuration();
				item2.startTime = mList.get(i).starttime;
				mNewList.add(item2);
				oldItem = mList.get(i);
			}
			/*************** 处理最后一个点 *************/
			if (mList.size() == 1) {
				NewRecordItem item = new NewRecordItem();
				item.isLeft = 1;
				item.startTime = oldItem.starttime;
				item.length = totalLength - oldItem.starttime;
				mNewList.add(item);
			} else {
				RecordItem lastItem = mList.get(mList.size() - 1);
				NewRecordItem item = new NewRecordItem();
				item.isLeft = 1;
				item.startTime = oldItem.starttime ;
				item.length =lastItem.starttime- oldItem.starttime ;
				mNewList.add(item);
				NewRecordItem item2 = new NewRecordItem();
				item2.isLeft = 0;
				item2.audio=lastItem;
				item2.length = lastItem.getDuration();
				item2.startTime = lastItem.starttime;
				mNewList.add(item2);
				if (lastItem.starttime != totalLength) {
					NewRecordItem item3 = new NewRecordItem();
					item3.isLeft = 1;
					item3.startTime = lastItem.starttime
							+ lastItem.getDuration();
					item3.length = totalLength - lastItem.starttime;
					mNewList.add(item3);
				}
			}
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (mNewList == null) {
			return null;
		}
		return mNewList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void DoStopAudioPlayer() {
		if (mPlayer != null) {
			mPlayer.DoStop();
			mSubPlayer.DoStop();
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		NewRecordItem item= mNewList.get(position);
		if (item.isLeft == 1)// 左边
		{
			convertView = mInflater.inflate(R.layout.item_revisers_result_left,
					null);
			holder = new ViewHolder();
			holder.imgAudio = (ImageView) convertView
					.findViewById(R.id.imgAudio);
			holder.llyAudio = (LinearLayout) convertView
					.findViewById(R.id.lly_audio);
			holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
			holder.llyAudio.setOnClickListener(new AnswerPlay());
		
		} else if (item.isLeft == 0)// 右边
		{
			convertView = mInflater.inflate(
					R.layout.item_revisers_result_right, null);
			holder = new ViewHolder();
			holder.imgAudio = (ImageView) convertView
					.findViewById(R.id.imgAudio);
			holder.llyAudio = (LinearLayout) convertView
					.findViewById(R.id.lly_audio);
			holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
			holder.llyAudio.setOnLongClickListener(new AudioLongClick());
			holder.llyAudio.setOnClickListener(new AudioPlay());
			
			holder.tvDes = (TextView) convertView.findViewById(R.id.tvTextRevise);
			holder.changeImg = (ImageView) convertView
			.findViewById(R.id.img_change);
			holder.changeImg.setTag(item);
			holder.changeImg.setOnClickListener(new InputChange());
			if(((RecordItem)item.audio).tvDes==null||((RecordItem)item.audio).tvDes.length()==0)
			{
				holder.tvDes.setVisibility(View.GONE);
			}else
			{
				holder.tvDes.setText(((RecordItem)item.audio).tvDes);
			}
		}
		int length = item.length / 1000;
		if (length == 0 && item.length > 0) {
			length = 1;
		}
		holder.tvTime.setText(length + "'");
		
		LayoutParams lp = holder.llyAudio.getLayoutParams();
		if (length > 60) {
			lp.width = (int) mScreenWidth;
		} else if (length <= 5) {
			lp.width = (int) (mScreenWidth * 0.8);
		} else {
			int w = (int) (((5 + length) / 60.0) * mScreenWidth * 0.8 + mScreenWidth * 0.8);
			lp.width = w;
		}
		holder.llyAudio.setTag(item);
		holder.llyAudio.setLayoutParams(lp);
		return convertView;
	}

	class AudioPlay implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			RecordItem audios = (RecordItem)((NewRecordItem) v.getTag()).audio;
			ImageView img = (ImageView) v.findViewById(R.id.imgAudio);
			img.setBackgroundResource(R.drawable.anim_revise_playing);
			if (statusAnim != null) {
				statusAnim.stop();
			}
			statusAnim = (AnimationDrawable) img.getBackground();
			statusAnim.start();
			mListener.stopAudio();
			mPlayer.DoStop();
			mSubPlayer.DoStop();
			mPlayer.ASycPlay(audios.file.getAbsolutePath(), mHandler);
			oldImg = img;
		}
	}
	class InputChange implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			RecordItem audios = (RecordItem)((NewRecordItem) v.getTag()).audio;
			mListener.showInput(audios);
			v.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_change_input_unable));
		}
	}
	class AnswerPlay implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			NewRecordItem audios =(NewRecordItem) v.getTag();
			ImageView img = (ImageView) v.findViewById(R.id.imgAudio);
			img.setBackgroundResource(R.drawable.anim_revise_left_playing);
			if (statusAnim != null) {
				statusAnim.stop();
			}
			statusAnim = (AnimationDrawable) img.getBackground();
			statusAnim.start();
			mSubPlayer.DoStop();
			mListener.stopAudio();
			mPlayer.DoStop();
			mSubPlayer.play(audios.startTime, audios.length);
			oldImg = img;
		}
	}
	class AudioLongClick implements OnLongClickListener {

		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			final RecordItem audios = (RecordItem)((NewRecordItem) v.getTag()).audio;
			new AlertDialog.Builder(context).setTitle("Delete Revise")
					.setMessage("Are You Sure To Delete This Revise")
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									mList.remove(audios);
									resetNewItems();
									ReviseAudioAdapter.this
											.notifyDataSetChanged();
								}
							}).setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

								}
							}).show();
			return false;
		}

	}

	protected Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {// 此方法在ui线程运行
			if (statusAnim != null) {
				statusAnim.stop();
			}
			ReviseAudioAdapter.this.notifyDataSetChanged();
		}
	};

	static class ViewHolder {
		TextView tvTime;
		LinearLayout llyAudio;
		ImageView imgAudio;
		TextView tvStartTime;
		
		ImageView changeImg;
		TextView tvDes;
	}

}
